package com.blueocean_health.care.management.mapper;

import com.blueocean_health.care.management.domain.base.NurseCheckWork;
import com.blueocean_health.care.management.domain.base.NurseGroup;
import com.blueocean_health.care.management.domain.checkWorkVo.*;
import com.blueocean_health.care.management.util.MyMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

public interface NurseCheckWorkMapper extends MyMapper<NurseCheckWork> {

    @Select("SELECT group_id ,group_name ,group_name_py FROM nurse_group WHERE deflag = 0 AND hospital_id =#{hid} ORDER BY group_name")
    List<NurseGroup> queryNurseGroupListByHospital(@Param("hid")String hid);

    /**
     * 考勤列表查询
     * @param checkWorkListDto
     * @return
     */
    List<CheckWorkListVo> queryCheckWorkList(CheckWorkListDto checkWorkListDto);

    /**
     * 新版考勤列表查询
     * @param CheckWorkListNewDto
     * @return CheckWorkListNewVo
     */
    List<CheckWorkListNewVo> queryCheckWorkNewList(CheckWorkListNewDto checkWorkListNewDto);

    /**
     * 考勤列表查询 包含临时小组成员
     * @param checkWorkListDto
     * @return
     */
    List<CheckWorkListVo> queryCheckWorkListAndTemporary(CheckWorkListDto checkWorkListDto);

    @Select("SELECT *FROM nurse_check_work WHERE deflag = 0 AND work_time =#{workTime} AND group_id =#{groupId}")
    List<NurseCheckWork> queryLastInfoByTimeAndGroup(@Param("workTime")String workTime,@Param("groupId")Integer groupId);

    @Update("UPDATE nurse_check_work SET deflag = 1 ,update_time = NOW() WHERE work_id =#{workId} AND work_time=#{workTime} AND group_id =#{groupId}")
    public Boolean deleteCheckWorkByGroupAndWorker(@Param("workTime")String workTime, @Param("workId")String workId, @Param("groupId")Integer groupId);

    @Update("UPDATE nurse_check_work SET work_status=#{workStatus} ,update_time = NOW() WHERE work_id =#{workId} AND work_time=#{workTime} AND group_id =#{groupId}")
    public Boolean updateCheckWorkByGroupAndWorker(@Param("workStatus")String workStatus,@Param("workTime")String workTime, @Param("workId")String workId, @Param("groupId")Integer groupId);

    @Select("SELECT " +
            "a.`work_time` AS workDate, " +
            "a.`group_id` AS groupId, " +
            "a.`work_status` AS workStatus, " +
            "b.`group_name` AS groupName " +
            "FROM " +
            "nurse_check_work a,nurse_group b " +
            "WHERE " +
            "a.`group_id` = b.`group_id` " +
            "AND a.work_id =#{workId} " +
            "AND a.deflag = 0 " +
            "AND a.work_time >= #{startDate} " +
            "AND a.work_time < #{endDate} ")
    List<SelectMonthVo> queryMonthCheckByWorker(@Param("workId")String workId, @Param("startDate")String startDate, @Param("endDate")String endDate);

    @Select("SELECT *FROM nurse_check_work WHERE deflag = 0 AND work_time =#{workTime}")
    List<NurseCheckWork> queryAllByWorkTime(@Param("workTime")String workTime);

    @Select("SELECT " +
            "* " +
            "FROM " +
            "nurse_check_work a " +
            "WHERE a.`deflag` = 0 " +
            "AND a.`work_time` >=#{startTime} " +
            "AND a.`work_time` <=#{endTime} " +
            "AND a.`work_status` = 0 ")
    List<NurseCheckWork> queryAllByStartAndEndTime(@Param("startTime")String startTime,@Param("endTime")String endTime);
    
    @Select("SELECT " +
    		"count(1) " +
    		"FROM " +
    		"nurse_check_work a " +
    		"WHERE a.`deflag` = 0 " +
    		"AND a.`work_time` >=#{startTime} " +
    		"AND a.`work_time` <=#{endTime} " +
    		"AND a.`work_status` = 0 AND work_id = #{workId}")
    Long queryByWorkerId(@Param("workId")String workId,@Param("startTime")String startTime,@Param("endTime")String endTime);
    @Select("SELECT " +
    		"count(1) " +
    		"FROM " +
    		"nurse_check_work a " +
    		"WHERE a.`deflag` = 0 " +
    		"AND a.`work_time` >=#{startTime} " +
    		"AND a.`work_time` <=#{endTime} " +
    		"AND a.`work_status` = 0 AND work_id = #{workId} AND group_id = #{groupId}")
    Integer queryByWorkIdAndGroupId(@Param("workId")String workId,@Param("groupId")String groupId,@Param("startTime")String startTime,@Param("endTime")String endTime);
    /**
     * 查询某段时间内 某小组除临时护工的总出勤天数 2018年6月11日11:08:04
     * @param groupId
     * @param startTime
     * @param endTime
     * @return
     */
    Long queryByGroupId(@Param("groupId")Integer groupId,@Param("startTime")String startTime,@Param("endTime")String endTime);

    @Select("SELECT *FROM nurse_check_work n WHERE n.`deflag` = 0 AND n.`group_id` =#{groupId}")
    List<NurseCheckWork> queryAllByGroupId(@Param("groupId")Integer groupId);

    void deleteWorkCheckByTime(@Param("startTime")Date startTime,@Param("endTime")Date endTime,@Param("workId")String workId);

    @Select("SELECT " +
            "a.*,b.`group_name` " +
            "FROM " +
            "nurse_check_work a ,nurse_group b " +
            "WHERE  " +
            "a.`group_id` = b.`group_id` " +
            "AND a.`deflag` = 0  " +
            "AND a.`work_time` >=#{startTime}  " +
            "AND a.`work_time` <=#{endTime}  " +
            "AND a.`work_id` =#{workId} ")
    List<CheckWorkListNewVo> queryInfoByWorkIdAndTime(@Param("startTime")String startTime,@Param("endTime")String endTime,@Param("workId")String workId);

    @Update("UPDATE nurse_check_work SET deflag = 1 ,update_time = NOW() WHERE work_id =#{workId} AND work_time=#{workTime}")
    public Integer updateCheckWorkNew(@Param("workTime")String workTime,@Param("workId")String workId);

}